<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
	#box{
		width: 300px;
		height: 200px;
		background: #3F48CC;
		position: absolute;
		top: 0;
		left: 0;
	}
</style>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
	
$(function(){
	var box = $('#box');//抓到元素

	box.mousedown(function(ev){
		// 获得鼠标的位置
		var m_old_left = ev.pageX;
		var m_old_top = ev.pageY;
		// 获得div的位置
		var box_old_left = box.position().left;
		var box_old_top = box.position().top;

		// 给文档区域加鼠标移动事件
		$(document).mousemove(function(ev){
			// 获得鼠标当前的位置
			var m_new_left = ev.pageX;
			var m_new_top = ev.pageY;

			// 计算鼠标移动的距离
			var left_diff = m_new_left - m_old_left;
			var top_diff = m_new_top - m_old_top;

			// 计算div新的位置
			var box_new_left = box_old_left + left_diff;
			var box_new_top = box_old_top + top_diff;

			// 给div设置新的位置
			box.css({'top':box_new_top+'px','left':box_new_left+'px'});
		})
	})

	box.mouseup(function() {
		$(document).off('mousemove');//鼠标抬起的时候，取消document身上的鼠标移动事件
	});
})

</script>
</head>

<body>
<div id="box"></div>
</body>
</html>
